<template>
  <div class="area-stats-container">
    <h3 class="stats-title">📊 面积统计</h3>
    
    <div class="stats-summary">
      <div class="total-area">
        <span class="label">总面积:</span>
        <span class="value">{{ totalArea }} px²</span>
      </div>
      <div class="painted-area">
        <span class="label">已涂色:</span>
        <span class="value">{{ paintedArea }} px²</span>
      </div>
      <div class="paint-percentage">
        <span class="label">涂色率:</span>
        <span class="value">{{ paintPercentage }}%</span>
      </div>
    </div>
    
    <div class="leaderboard">
      <h4 class="leaderboard-title">🏆 排行榜</h4>
      
      <div class="players-list">
        <div 
          v-for="(player, index) in sortedPlayers" 
          :key="player.id"
          class="player-row"
          :class="{ 
            'current-player': player.id === currentPlayerId,
            'winner': index === 0 && gameFinished
          }"
        >
          <div class="player-rank">
            <span class="rank-number">{{ index + 1 }}</span>
            <div class="rank-medal" v-if="index < 3">
              {{ getMedal(index) }}
            </div>
          </div>
          
          <div class="player-info">
            <div class="player-name">
              {{ player.name }}
              <span v-if="player.id === currentPlayerId" class="you-badge">你</span>
            </div>
            <div class="player-color" :style="{ backgroundColor: player.color }"></div>
          </div>
          
          <div class="player-stats">
            <div class="area-count">
              {{ player.areaCount || 0 }} px²
            </div>
            <div class="area-percentage">
              {{ getPlayerPercentage(player.areaCount || 0) }}%
            </div>
          </div>
          
          <div class="player-progress">
            <div class="progress-bar">
              <div 
                class="progress-fill"
                :style="{ 
                  width: getPlayerPercentage(player.areaCount || 0) + '%',
                  backgroundColor: player.color
                }"
              ></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div v-if="gameFinished" class="game-result">
      <div class="winner-announcement">
        <h3>🎉 游戏结束！</h3>
        <div class="winner-info">
          <span class="winner-label">获胜者:</span>
          <span class="winner-name">{{ winner?.name }}</span>
          <div class="winner-color" :style="{ backgroundColor: winner?.color }"></div>
        </div>
        <div class="winner-score">
          最终得分: {{ winner?.areaCount || 0 }} px²
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  players: {
    type: Array,
    default: () => []
  },
  currentPlayerId: {
    type: String,
    default: ''
  },
  gameFinished: {
    type: Boolean,
    default: false
  },
  canvasWidth: {
    type: Number,
    default: 800
  },
  canvasHeight: {
    type: Number,
    default: 600
  }
})

const totalArea = computed(() => {
  return props.canvasWidth * props.canvasHeight
})

const paintedArea = computed(() => {
  return props.players.reduce((total, player) => {
    return total + (player.areaCount || 0)
  }, 0)
})

const paintPercentage = computed(() => {
  if (totalArea.value <= 0) return 0
  return Math.round((paintedArea.value / totalArea.value) * 100)
})

const sortedPlayers = computed(() => {
  return [...props.players].sort((a, b) => {
    return (b.areaCount || 0) - (a.areaCount || 0)
  })
})

const winner = computed(() => {
  if (props.gameFinished && sortedPlayers.value.length > 0) {
    return sortedPlayers.value[0]
  }
  return null
})

function getMedal(index) {
  const medals = ['🥇', '🥈', '🥉']
  return medals[index] || ''
}

function getPlayerPercentage(areaCount) {
  if (totalArea.value <= 0) return 0
  return Math.round((areaCount / totalArea.value) * 100)
}
</script>

<style scoped>
.area-stats-container {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  max-width: 400px;
}

.stats-title {
  margin: 0 0 1.5rem 0;
  color: #333;
  font-size: 1.2rem;
  text-align: center;
}

.stats-summary {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.total-area, .painted-area, .paint-percentage {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.label {
  font-weight: 500;
  color: #666;
}

.value {
  font-weight: 600;
  color: #333;
  font-family: monospace;
}

.leaderboard {
  margin-bottom: 1.5rem;
}

.leaderboard-title {
  margin: 0 0 1rem 0;
  color: #333;
  font-size: 1.1rem;
  text-align: center;
}

.players-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.player-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem;
  border-radius: 8px;
  border: 2px solid #e9ecef;
  transition: all 0.3s ease;
  position: relative;
}

.player-row.current-player {
  border-color: #007bff;
  background-color: #f8f9ff;
}

.player-row.winner {
  border-color: #ffd700;
  background: linear-gradient(135deg, #fffbf0, #fff3cd);
  box-shadow: 0 4px 15px rgba(255, 215, 0, 0.3);
}

.player-rank {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  min-width: 40px;
}

.rank-number {
  font-size: 1.2rem;
  font-weight: bold;
  color: #333;
}

.rank-medal {
  font-size: 1.5rem;
}

.player-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.player-name {
  font-weight: 600;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.you-badge {
  background: #007bff;
  color: white;
  padding: 0.2rem 0.5rem;
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: normal;
}

.player-color {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
}

.player-stats {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.25rem;
  min-width: 80px;
}

.area-count {
  font-weight: 600;
  color: #333;
  font-family: monospace;
  font-size: 0.9rem;
}

.area-percentage {
  font-size: 0.8rem;
  color: #666;
}

.player-progress {
  width: 60px;
  height: 8px;
  background: #e9ecef;
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  transition: width 0.5s ease;
  border-radius: 4px;
}

.game-result {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border-radius: 8px;
  padding: 1.5rem;
  text-align: center;
}

.winner-announcement h3 {
  margin: 0 0 1rem 0;
  font-size: 1.5rem;
}

.winner-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.winner-label {
  font-weight: 500;
}

.winner-name {
  font-weight: 600;
  font-size: 1.2rem;
}

.winner-color {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.winner-score {
  font-size: 1.1rem;
  font-weight: 500;
  opacity: 0.9;
}

@media (max-width: 768px) {
  .area-stats-container {
    max-width: 100%;
  }
  
  .player-row {
    flex-wrap: wrap;
    gap: 0.5rem;
  }
  
  .player-stats {
    min-width: 60px;
  }
  
  .player-progress {
    width: 100%;
    order: 4;
  }
}
</style> 